package com.zyl.controller;

import org.springframework.beans.factory.annotation.Autowired;

import com.zyl.entity.Sections;
import com.zyl.service.SectionsService;
import com.zyl.util.Result;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;




@RestController
public class SectionsController {
	@Autowired
	private SectionsService sectionsService;

	/**
	 * 查询
	 */
	@RequestMapping("/getTypeAll")
	public List<Sections> getTypeAll() {
		return sectionsService.list();

	}

	/**
	 * 分页查询
	 * 
	 * @return
	 */
	@RequestMapping("/page")
	public Result testSelectPage(Integer page, Integer limit, String name) {
		// 创建查询条件构造器
		QueryWrapper<Sections> wrapper = new QueryWrapper<>();
		if (name != null) {
			wrapper.like("name", name);
		}
		Page<Sections> page1 = new Page<Sections>(page, limit);// page limit
		Page<Sections> pages = sectionsService.page(page1, wrapper);
		// 获取当前总数据
		List<Sections> records = pages.getRecords();
		// 获取当前总条数
		int total = (int) pages.getTotal();
		Result r;
		if (pages != null) {
			r = new Result(0, "", total, records);
		} else {
			r = new Result(1, "无数据");
		}
		return r;

	}

	@RequestMapping("/addSections")
	public Result addSections(Sections se) {
		Result r;
		if (sectionsService.save(se)) {
			r = new Result(0, "添加成功");
		} else {
			r = new Result(1, "添加失败");
		}
		return r;
	}

	@RequestMapping("/updateSections")
	public Result updateSections(Sections se) {
		Result r;
		if (sectionsService.saveOrUpdate(se)) {
			r = new Result(0, "更新成功");
		} else {
			r = new Result(1, "更新失败");
		}
		return r;

	}

	/**
	 * 批量删
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delSections")
	public Map<String, Object> testDeleteByIds(@RequestParam List<Integer> sectionid) {

		boolean removeByIds = sectionsService.removeBatchByIds(sectionid);
		Map<String, Object> results = new HashMap<>();
		if (removeByIds) {
			results.put("code", 0);
			results.put("msg", "删除成功");
		} else {
			results.put("code", 1);
			results.put("msg", "删除失败");
		}
		return results;

	}
	
}

